package com.nono.Dao;

import java.security.PublicKey;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import com.nono.bean.Commodity;
import com.nono.bean.Type;

@Repository
public class AdminDao {
	@Autowired
	JdbcDao jdbcDao;
	
	/**
	 * @desc 类型
	 * return 返回一个数组;
	 * */
	public List<Type> getTypes() {
		ArrayList<Type> list = new ArrayList<Type>();
		List<Map<String,Object>> rows = jdbcDao.jdbcTemplate.queryForList("select * from types");
		//, new Object[] {studentId,subjectId}
		Iterator iterator = rows.iterator();
		while( iterator.hasNext() ) {
			Type type = new Type();
			Map map = (Map) iterator.next();
			type.setId( (Integer)map.get("id") );
			type.setName( (String)map.get("name") );
			list.add( type );
		};
		return list;
	}
	
	/**
	 * @desc 类型
	 * return boolean如果成功的话返回true，否则返回false;
	 * */
	public boolean addType( String type ) {
		return 0!=jdbcDao.jdbcTemplate.update("insert into types values(null,?)",new Object[] {type});
	}
	
	public boolean delType( String id ) {
		return 0!=jdbcDao.jdbcTemplate.update("delete from types where id=?",new Object[] {id});
	}
	
	
	/**
	 * @desc 产品
	 * @desc 增加产品;
	 * @resturn boolean;
	 * */
	public boolean addPro(String name, String depict, int price, int amount, String manufacturer, String img, String type) {
		return 0!=jdbcDao.jdbcTemplate.update("insert into commodity values(null,?,?,?,?,?,?,?)",new Object[] {name, depict, price, amount, manufacturer, img, type});
	}
	
	/**
	 * @desc 删除产品
	 * */
	public boolean delPro(int id) {
		return 0!=jdbcDao.jdbcTemplate.update("delete  from  commodity  where id=?",new Object[] {id});
	}
	
	/**
	 * @desc 产品
	 * @desc 获取所有的产品
	 * */
	public List<Commodity> getAllCom() {
		NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(jdbcDao.jdbcTemplate);
		Map<String, Object> paramMap = new HashMap<String, Object>();  
		RowMapper< Commodity > rm = new BeanPropertyRowMapper<Commodity>( Commodity.class );
		List<Commodity> commoditys = namedParameterJdbcTemplate.query("select * from commodity",paramMap,rm);
		return commoditys;
	}
	
	/**
	 * @desc获取指定id的产品;
	 * */
	public Commodity getComById(int id) {

		RowMapper< Commodity > rm = new BeanPropertyRowMapper<Commodity>( Commodity.class );
		 return jdbcDao.jdbcTemplate.queryForObject("select *  from  commodity  where id=?",new Object[] {id}, rm);
	}
	
	/**
	 * @desc 获取某种类型的产品
	 * @param name 类别名称
	 * @return List;
	 * */
	public List<Commodity> getComByType(String type) {
		NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(jdbcDao.jdbcTemplate);
		Map<String, Object> paramMap = new HashMap<String, Object>();  
		paramMap.put("type", type);
		RowMapper< Commodity > rm = new BeanPropertyRowMapper<Commodity>( Commodity.class );
		List<Commodity> commoditys = namedParameterJdbcTemplate.query("select * from commodity where type=(:type)",paramMap,rm);
		return commoditys;
	}
	
	/**
	 * @search商品
	 * */
	public List<Commodity> search(String keyword) {
		NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(jdbcDao.jdbcTemplate);
		Map<String, Object> paramMap = new HashMap<String, Object>();  
		RowMapper< Commodity > rm = new BeanPropertyRowMapper<Commodity>( Commodity.class );
		List<Commodity> commoditys = namedParameterJdbcTemplate.query("select * from commodity where name like '%"+keyword+"%' union( select * from commodity where depict like '%"+keyword+"%')",paramMap,rm);
		return commoditys;
	}
}
